
package com.trial.area.coupon.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trial.area.coupon.CouponUtils;
import com.trial.area.coupon.dao.CouponDao;
import com.trial.area.coupon.entity.CouponVO;
import com.trial.area.coupon.param.CouponParam;

/**
 * 优惠券Service实现类
 *
 * @author 朱国军
 * @version 2016年07月17日 朱国军
 */
@Service
public class CouponService {
    
    private Logger logger = LoggerFactory.getLogger(CouponService.class);
    
    @Autowired
    private CouponDao couponDao;
    
    /**
     * 修改
     *
     * @param entity 要修改的优惠券对象
     * @return 返回修改的条数
     */
    public Integer update(CouponVO entity) {
        return couponDao.update(entity);
    }

    /**
     * 根据ID获取优惠券实体
     *
     * @param id 主键
     * @return 优惠券信息
     */
    public CouponVO findById(String id) {
        return couponDao.findById(id);
    }

    /**
     * 按查询条件【分页】查询
     *
     * @param param 优惠券查询条件
     * @return 优惠券列表
     */
    public List<CouponVO> queryByPage(CouponParam param) {
        return couponDao.queryByPage(param);
    }
    
    /**
     * 根据用户ID查询优惠券信息
     *
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return
     */
    public CouponVO findByUserId(String userId, String goodsId) {
        return couponDao.findByUserId(userId, goodsId);
    }

    /**
     * 根据券码查询未兑换的优惠券
     *
     * @param couponNo 券码
     * @return
     */
    public CouponVO findByCouponNo(String couponNo) {
        return couponDao.findByCouponNo(couponNo);
    }
    
    /**
     * 批量生成券码
     *
     * @param couponName 券码名称
     * @param couponBatch 批次号
     * @param acquiringWay 获取途径说明
     * @param couponNum 生成的数量
     * @param couponPre 券码前缀
     * @return
     */
    public Integer batchCreate(String couponName, String couponBatch, String acquiringWay, Integer couponNum, String couponPre,
        Integer applicationScope, String applicationGoodsName, String applicationGoods, BigDecimal extraFee) {
        Integer successNo = 0;
        for (int i = 0; i < couponNum; i++) {
            Integer resultNum = createCoupon(couponName, couponBatch, acquiringWay, couponPre, applicationScope, applicationGoodsName,
                applicationGoods, extraFee);
            successNo = successNo + resultNum;
        }
        return successNo;
    }

    private Integer createCoupon(String couponName, String couponBatch, String acquiringWay, String couponPre, Integer applicationScope,
        String applicationGoodsName, String applicationGoods, BigDecimal extraFee) {
        Integer insertNum = 0;
        try {
            // 邀请活动-注册送券
            CouponVO registerCouponVO = new CouponVO();
            registerCouponVO.setAcquiringWay(acquiringWay);
            registerCouponVO.setCouponBatch(couponBatch);
            if (applicationScope == null || applicationScope == 1) {
                registerCouponVO.setApplicationScope(1);
            } else if (applicationScope == 2) {
                registerCouponVO.setApplicationScope(2);
                registerCouponVO.setApplicationGoodsName(applicationGoodsName);
                registerCouponVO.setApplicationGoods(applicationGoods);
            }
            registerCouponVO.setExtraFee(extraFee);
            registerCouponVO.setCouponId(UUID.randomUUID().toString());
            registerCouponVO.setCouponName(couponName);
            registerCouponVO.setCouponNo(CouponUtils.createCouponNo(couponPre));
            registerCouponVO.setCouponState(1);
            registerCouponVO.setDataStatus(0);
            registerCouponVO.setDiscountAmount(new BigDecimal(100));
            registerCouponVO.setDiscountType(1);
            insertNum = couponDao.insert(registerCouponVO);
        } catch (Exception e) {
            logger.error("创建优惠券出错", e);
        }
        return insertNum;
    }

    /**
     * 根据条件查询券码列表
     *
     * @param couponParam 券码查询条件
     * @return
     */
    public List<Map<String, Object>> queryMapByPage(CouponParam couponParam) {
        return couponDao.queryMapByPage(couponParam);
    }

    /**
     * 查询每个批次的数量
     *
     * @param couponParam 查询条件
     * @return
     */
    public List<Map<String, Object>> queryBatchByPage(CouponParam couponParam) {
        return couponDao.queryBatchByPage(couponParam);
    }
}
